package com.tian.view.anim.view_anim

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.view.animation.ScaleAnimation
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.tian.view.R
import com.tian.view.databinding.ViewFourBottonBinding

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/14 10:04
 *
 * 缩放动画
 */
class ScaleAnimFragment : Fragment() {

    private lateinit var b: ViewFourBottonBinding


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        b = ViewFourBottonBinding.inflate(inflater)
        return b.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireActivity() as AppCompatActivity).supportActionBar?.title = "Scale缩放动画"
        b.mbtOne.text = "xml定义Scale动画"

        val listener = object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {
                Toast.makeText(requireContext(), "anim start", Toast.LENGTH_SHORT).show()
            }

            override fun onAnimationEnd(animation: Animation?) {
                Toast.makeText(requireContext(), "anim end", Toast.LENGTH_SHORT).show()
            }

            override fun onAnimationRepeat(animation: Animation?) {
                Toast.makeText(requireContext(), "anim repeat", Toast.LENGTH_SHORT).show()
            }
        }

        // AnimationUtils可以加载 xml 配置动画
        // scale 的xml定义解释在 scale_anim_1.xml中
        val anim = AnimationUtils.loadAnimation(requireContext(), R.anim.scale_anim_1)
        anim.setAnimationListener(listener)
        b.mbtOne.setOnClickListener {
            b.mbtOne.startAnimation(anim)
        }

        b.mbtTwo.text = "java代码Scale"
        b.mbtTwo.setOnClickListener {
            /**
             * pivotXType: 的值对应xml中设置值的类型
             * [Animation.ABSOLUTE] 对应的数值绝对值 eg: 50
             * [Animation.RELATIVE_TO_SELF] 对应百分值
             * [Animation.RELATIVE_TO_PARENT] 对应百分值+p
             */
            val anim = ScaleAnimation(0.5f, 1.5f, 0.4f, 1.4f,
                    Animation.RELATIVE_TO_PARENT, 0.2f, Animation.ABSOLUTE, 20f)
            anim.duration = 1400
            anim.setAnimationListener(listener)
            b.mbtTwo.startAnimation(anim)
        }
        b.mbtThree.visibility = View.GONE
        b.mbtFour.visibility = View.GONE
    }


}